import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import '../models/ingredient.dart';

class IngredientDetailScreen extends StatefulWidget {
  final Ingredient ingredient;

  const IngredientDetailScreen({super.key, required this.ingredient});

  @override
  State<IngredientDetailScreen> createState() => _IngredientDetailScreenState();
}

class _IngredientDetailScreenState extends State<IngredientDetailScreen> {
  bool isFavorite = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.ingredient.name),
        elevation: 0,
        actions: [
          IconButton(
            icon: Icon(isFavorite ? Icons.favorite : Icons.favorite_border),
            onPressed: () {
              setState(() => isFavorite = !isFavorite);
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text(isFavorite ? '已添加到收藏' : '已取消收藏'),
                  duration: const Duration(seconds: 1),
                ),
              );
            },
          ),
          IconButton(
            icon: const Icon(Icons.share),
            onPressed: () {
              Share.share(
                '${widget.ingredient.name}\n\n${widget.ingredient.description}\n\n'
                '使用方法：${widget.ingredient.usage.join('、')}',
              );
            },
          ),
        ],
      ),
      body: Stack(
        children: [
          SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 头部图片
                Hero(
                  tag: 'ingredient_${widget.ingredient.name}',
                  child: Container(
                    height: 200,
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.grey[200],
                      image: widget.ingredient.imageUrl.isNotEmpty
                          ? DecorationImage(
                              image: NetworkImage(widget.ingredient.imageUrl),
                              fit: BoxFit.cover,
                            )
                          : null,
                    ),
                    child: widget.ingredient.imageUrl.isEmpty
                        ? const Icon(Icons.food_bank, size: 64)
                        : null,
                  ),
                ),
                // 基本信息卡片
                Card(
                  margin: const EdgeInsets.all(16),
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              widget.ingredient.name,
                              style: const TextStyle(
                                fontSize: 24,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            const Spacer(),
                            Chip(
                              label: Text(widget.ingredient.category),
                              backgroundColor: Theme.of(context).primaryColor.withOpacity(0.1),
                              labelStyle: TextStyle(
                                color: Theme.of(context).primaryColor,
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 16),
                        Text(
                          widget.ingredient.description,
                          style: TextStyle(
                            fontSize: 16,
                            color: Colors.grey[700],
                            height: 1.5,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                // 使用方法卡片
                Card(
                  margin: const EdgeInsets.symmetric(horizontal: 16),
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            const Icon(Icons.restaurant, color: Colors.orange),
                            const SizedBox(width: 8),
                            const Text(
                              '使用方法',
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 16),
                        Wrap(
                          spacing: 8,
                          runSpacing: 8,
                          children: widget.ingredient.usage.map((use) => Chip(
                            label: Text(use),
                            backgroundColor: Colors.grey[200],
                          )).toList(),
                        ),
                      ],
                    ),
                  ),
                ),
                // 营养成分卡片
                Card(
                  margin: const EdgeInsets.all(16),
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            const Icon(Icons.pie_chart, color: Colors.green),
                            const SizedBox(width: 8),
                            const Text(
                              '营养成分',
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 16),
                        ...widget.ingredient.nutrition.entries.map((entry) => Padding(
                          padding: const EdgeInsets.only(bottom: 12),
                          child: Row(
                            children: [
                              Text(
                                entry.key,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.grey,
                                ),
                              ),
                              const Spacer(),
                              Text(
                                entry.value,
                                style: const TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ],
                          ),
                        )).toList(),
                      ],
                    ),
                  ),
                ),
                // 底部留白，防止被底部操作栏遮挡
                const SizedBox(height: 80),
              ],
            ),
          ),
          // 底部操作栏
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.1),
                    blurRadius: 10,
                    offset: const Offset(0, -2),
                  ),
                ],
              ),
              child: Row(
                children: [
                  Expanded(
                    child: OutlinedButton.icon(
                      onPressed: () {
                        // TODO: 添加到购物清单
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('已添加到购物清单')),
                        );
                      },
                      icon: const Icon(Icons.add_shopping_cart),
                      label: const Text('加入购物清单'),
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: ElevatedButton.icon(
                      onPressed: () {
                        // TODO: 查看相关菜谱
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('正在开发中...')),
                        );
                      },
                      icon: const Icon(Icons.restaurant_menu),
                      label: const Text('相关菜谱'),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}